/***************************************************************************
 *   Copyright (C) 2008 by Tim Sutton   *
 *   tim@linfiniti.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "window.h"
#include "imageeffect.h"
#include <QDirIterator>
#include <QPixmap>
Window::Window(QWidget* parent, Qt::WFlags fl)
    : QDialog(parent,fl)
{
  //required by Qt4 to initialise the ui
  setupUi(this);
  fillList();
  fillEffectsCombo();
  connect (lstThumbnails, SIGNAL( itemClicked ( QListWidgetItem * )), 
           this,SLOT (showFullItem( QListWidgetItem * )));
}

Window::~Window()
{
  //any cleanups you want go here
}
void Window::fillEffectsCombo()
{
  ImageEffect myEffect;
  cboEffects->insertItems(0,myEffect.availableEffects());
}
void Window::fillList()
{
  QStringList myFilters;
  myFilters << "*.jpg";
  myFilters << "*.JPG";
  // QDir::Files   List directories that match the filters.
  // QDir::AllDirs List all directories; i.e. don't apply the filters to directory names.
  QDirIterator myIterator(QApplication::applicationDirPath(), 
      myFilters,
      QDir::Files | QDir::AllDirs, 
      QDirIterator::Subdirectories
      );
  while (myIterator.hasNext()) 
  {
    QString myFileName = myIterator.next();
    QFileInfo myFileInfo(myFileName);
    if (!myFileInfo.isFile()) continue; //check the file exists
    QPixmap myPixmap(myFileName);
    QListWidgetItem * mypItem = new QListWidgetItem(myFileInfo.baseName());
    mypItem->setIcon(myPixmap);
    mypItem->setData(Qt::UserRole,myFileName);
    lstThumbnails->addItem(mypItem); //passes ownership to the list!
  }
}
  
void Window::on_sliderIconSize_valueChanged(int theValue)
{
  lstThumbnails->setIconSize(QSize(theValue,theValue));
}

void Window::showFullItem( QListWidgetItem * thepItem )
{
  QString myFileName = thepItem->data(Qt::UserRole).toString();
  QPixmap myPixmap(myFileName);
  ImageEffect myEffect;
  myPixmap = myEffect.applyEffect(cboEffects->currentText(),myPixmap);
  lblFullImage->setPixmap(myPixmap);
}
